package com.wing.file.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class TengXunFileUtil {

    @Value("${file.tengxun.region}")
    private String region;

    @Value("${file.tengxun.appId}")
    private String appId;

    @Value("${file.tengxun.accessKey}")
    private String accessKey;

    @Value("${file.tengxun.secretKey}")
    private String secretKey;

    public volatile static COSClient cosClient = null;

    public COSClient getCosClient() {
        if(null == null) {
            synchronized (COSClient.class) {
                if(null == null) {
                    // 初始化用户身份信息(secretId, secretKey)
                    COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
                    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
                    ClientConfig clientConfig = new ClientConfig(new Region(region));
                    // 生成cos客户端
                    cosClient = new COSClient(cred, clientConfig);
                }
            }
        }
            return cosClient;

    }

}
